// index.js
// 获取应用实例
const app = getApp()
const { getmiao, getping } = require('../../http/api')
const API=require('../../http/api')
Page({
  data: {
    lun:[],
    gong:[],
    title:'',
    miao:[],
    tui:[],
    kan:[],
    ping:[],
    list:[],
      page:1,
      pageSize:6,
      iss:true,
      di:false
  },
  goxq(e){
    console.log(e)
    const id=e.currentTarget.dataset.id
     wx.navigateTo({
       url: '/pages/xq/xq?id='+id,
     })
  },
  gofen(e){
     console.log(e)
     const fenid=e.currentTarget.dataset.id
     const fenindex=e.currentTarget.dataset.index
     wx.setStorageSync('fenid', fenid)
     wx.setStorageSync('fenindex', fenindex)
     wx.switchTab({
       url: '/pages/fen/fen',
     })
  },
  goyou(){
     wx.switchTab({
       url: '/pages/you/you',
     })
  },
  gosou(){
     wx.navigateTo({
       url: '/pages/sou/sou',
     })
  },
  // 事件处理函数
 yh(){
   console.log(this.data.txt)
   wx.navigateTo({
     url: '/pages/youhui/youhui',
   })
 },
  onShow:function(){
     this.getbanner()
     this.getfen()
     this.getgong()
     this.getms()
     this.gett()
     this.getkanjia()
     this.getping()
     this.getlist()
  },
  // 商品列表
  async getlist(){
    this.data.iss=false
    const page=this.data.page
    const pageSize=this.data.pageSize
    const res=await API.getlist({page,pageSize})
   console.log(res)
   if(res.code==0){
     this.data.iss=true
     this.data.list=this.data.list.concat(res.data.result)
    this.setData({
      list:this.data.list
    })
   }else if(res.code==700){
     this.data.di=true
     this.setData({
      di:this.data.di
     })
   }
  },
  onReachBottom(){
    if(this.data.iss){
      this.data.page++
      this.getlist()
    }
  },
  // 拼团
  async getping(){
     const res=await API.getping()
    //  console.log(res)
     this.setData({
       ping:res.data.result
     })
  },
  // 砍价
  async getkanjia(){
    const res=await API.getkan()
    // console.log(res)
    this.setData({
      kan:res.data.result
    })
  },
  // 爆品推荐
  async gett(){
    const res=await API.gettui()
    // console.log(res)
    this.setData({
      tui:res.data.result
    })
  },
  // 限时秒杀
  async getms(){
    const res=await API.getmiao()
    // console.log(res)
    this.setData({
       miao:res.data.result
    })
  },
  // 公告
  async getgong(){
    const res=await API.getgg()
    this.setData({
        txt:res.data.content,
        title:res.data.title
    })
    // console.log(res)
 },
  // 分类宫格
  async getfen(){
    const res=await API.getfen()
    // console.log(res)
    if(res.code==0){
      this.setData({
        gong:res.data
      })
    }
  },
  // 轮播图
  async getbanner(){
    const res=await API.getBannerAPI()
    // console.log(res)
    if(res.code==0){
      this.setData({
        lun:res.data
      })
    }
 },
})
